package com.ztewatch.wearcontacts;

import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Groups;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.ztewatch.wearcontacts.loader.GroupListLoader;
import com.ztewatch.wearcontacts.view.ContactCursorAdapter;
import com.ztewatch.wearcontacts.view.GroupListCursorAdapter;

public class WearContactGroupListActivity extends Activity {
	public static final int REQUEST_CODE = 123;
	public static final String GROUP_TITLE = "title";
	public static final String GROUP_DETAIL_NUM = "group_contact_num";
	private GroupListCursorAdapter mGroupAdapter;
	private GridView mScrollGridView;
	private boolean save_group_flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wear_contact_group_list);
		initGroupList();
	}

	private void initGroupList() {
		mScrollGridView = (GridView) findViewById(R.id.group_gridview);
		mGroupAdapter = new GroupListCursorAdapter(this, null);
		mScrollGridView.setAdapter(mGroupAdapter);
		mScrollGridView
				.setOnItemClickListener(new GroupListItemClickListener());
		mScrollGridView.setSelected(true);
	}

	private class GroupListItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			if(!(boolean)(view.getTag(ContactCursorAdapter.IS_CLICK_FLAG))){
				return;
			}
			Cursor cursor = (Cursor) mGroupAdapter.getItem(position);
			if (cursor != null) {
				int group_id = cursor.getInt(GroupListLoader.GROUP_ID);
				int group_contact_num = cursor
						.getInt(GroupListLoader.MEMBER_COUNT);
				String title = cursor.getString(GroupListLoader.TITLE);
				Uri groupUri = null;
				if (group_id != 0) {
					groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI,
							group_id);
				}

				Intent intent = new Intent();
				intent.setData(groupUri);
				Bundle bundle = new Bundle();
				bundle.putString(GROUP_TITLE, title);
				bundle.putInt(GROUP_DETAIL_NUM, group_contact_num);
				intent.putExtras(bundle);
				intent.setClass(WearContactGroupListActivity.this,
						WearContactEditGroupActivity.class);
				startActivityForResult(intent, REQUEST_CODE);
			}
		}

	}

	@Override
	protected void onStart() {
		super.onStart();
		getLoaderManager().initLoader(0, null, mGroupLoaderListener);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			save_group_flag = bundle.getBoolean(
					WearContactEditGroupActivity.SAVE_GROUP_FLAG, false);
		}
	}

	/**
	 * The listener for the group meta data loader for all groups.
	 */
	private final LoaderManager.LoaderCallbacks<Cursor> mGroupLoaderListener = new LoaderCallbacks<Cursor>() {

		@Override
		public CursorLoader onCreateLoader(int id, Bundle args) {
			return new GroupListLoader(WearContactGroupListActivity.this);
		}

		@Override
		public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
			mGroupAdapter.swapCursor(data);
			int count = data.getCount();
			if (save_group_flag && count != 0) {
				mScrollGridView.setSelection(count - 1);
			}
		}

		@Override
		public void onLoaderReset(Loader<Cursor> loader) {
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getLoaderManager().destroyLoader(0);
	}
}
